<template>
	<view class="projectEmploy-container">
		<view class="">
			<view class="cu-bar search">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input @input="InputFocus" @blur="InputBlur" v-model="searchName" :adjust-position="false" type="text"
						placeholder="搜索项目名称" confirm-type="search" />
				</view>
				<!-- <view class="action">
					<button class="cu-btn bg-green shadow-blur round" @tap="searchList">搜索</button>
				</view> -->
			</view>
		</view>

		<view class="tabSwitch">
			<scroll-view scroll-x class="nav" scroll-with-animation :scroll-left="scrollLeft">
				<view class="cu-item" :class="item.id == TabCur ? 'text-green cur' : ''" v-for="item in tabList" :key="item.id"
					@tap="tabSelect" :data-id="item.id">
					{{ item.name }}
				</view>
			</scroll-view>
		</view>
		<view class="taskList" style="height: 100vh;" v-if="projectList.length == 0">
			<view class="cutpage-container">
				<!-- <view class="load-more" @tap="loadBack">上一页</view> -->
				<view class="no-more">暂无该项目</view>
			</view>
		</view>
		<view class="taskList" v-else>
			<view class="task-item" v-for="item in projectList" :key="item.id" @tap="gotoDetail(item.id)">
				<view class="item-top">
					<view class="item-title">{{ item.name }}</view>
					<view class="item-money">{{ item.price }}<span>元</span></view>
				</view>
				<view class="item-content">

					<!-- 富文本 -->
					<!-- <rich-text :nodes="item.content" space="nbsp"></rich-text> -->
				</view>
				<view class="tagsList">
					<view class="tags" v-for="(keyword, kIndex) in item.keywords.split(',')" :key="kIndex"
						v-if="item.keywords !== ''">{{ keyword }}</view>
				</view>
				<view class="publisher-container">
					<view class="publisher-avator" @tap.stop="$openrul(`/pages/userInfo/userInfo?userid=${item.create_user.id} }`)">
						<view class="cu-avatar round" :style="{ 'background-image': `url(${item.create_user.avatar})` }">
						</view>
						<span>{{ item.create_user.username }}</span>
					</view>
					<view class="publisher-location">
						<text>{{ item.city }}</text>
					</view>
				</view>
			</view>
			<!-- 分页 -->
			<view class="cutpage-container">
				<!-- <view class="load-more" @tap="loadBack">上一页</view> -->
				<view v-if="hasMore" class="load-more" @tap="loadNext">加载更多</view>
				<view v-else class="no-more">没有更多数据了</view>
			</view>
		</view>

		<!-- 发布信息按钮 -->
		<view class="sendBtn-contaienr" @tap="publishProject">
			<image src="/static/img/sendBtn.png" mode="scaleToFill" />
		</view>

	</view>
</template>

<script>
	import loginjs from '../../core/login'
export default {
	data() {
		return {
			searchBeforeList: [],
			timer: null,
			searchName: '',
			hasMore: true,
			page: 1, // 当前页数
			limit: 10, // 每页显示条目数
			projectList: [],
			TabCur: 1,
			scrollLeft: 0,
			tabList: [{
				id: 1,
				name: '全部'
			},
			{
				id: 2,
				name: '热门'
			},
			]

		};
	},
	  onShow(){
	    this.islogin()
	
	},
	onLoad() {
		this.getProjectList()
	},
	methods: {
		   islogin() {
		
		      if (loginjs.checkIfLogin() == false) {
		        this.loginFlag = false
		        // 弹窗提示请登录后操作
		        uni.showToast({
		          title: '请先登录',
		          icon: 'none',
		          mask: true,
		          duration: 1000,
		        })
		
		        uni.navigateTo({
		          url: '/pages/auth/login/index'
		
		        })
		
		      }
		},
		async getProjectList(isHis) {
			this.hasMore = true
			let res = await this.$myHttp.post({
				url: this.$myHttp.urlMap.getProjectList,
				data: {
					isHis: isHis,
					page: 1,
					limit: this.limit,
				}
			})


			// 使用 Set 来存储已经存在的数据
			let existingData = new Set(this.projectList.map(item => item.id));

			// 过滤掉已经存在的数据
			let newData = res.data.data.filter(item => !existingData.has(item.id));

			// 将新数据添加到项目列表中
			this.projectList = this.projectList.concat(newData);
			console.log('%c [ this.projectList ]-124', 'font-size:13px; background:pink; color:#bf2c9f;', this.projectList)



			this.projectListTotal = res.data.total
			if (this.projectList.length >= this.projectListTotal) {
				this.hasMore = false
			}





		},
		InputFocus(e) {
			clearTimeout(this.timer);
			// 清除之前的定时器
			// 创建一个新的定时器
			this.timer = setTimeout(() => {
				this.searchList(e.detail.value)
			}, 500); // 设置防抖延迟时间为500毫秒
		},
		async searchList(keyword) {

			this.hasMore = false

			const newpage = 1
			// this.InputBottom = 0
			if (this.searchName !== '') {
				let res = await this.$myHttp.post({
					url: this.$myHttp.urlMap.getProjectList,
					data: {
						name: this.searchName,
						isHis: false,
						page: newpage,
						limit: 1000,
					}
				})

				this.projectList = res.data.data
				console.log('searchAfter,this.projectList: ', this.projectList)

				this.projectListTotal = res.data.total
			}


			// 使用 Set 来存储已经存在的数据
			// let existingData = new Set(this.projectList.map(item => item.id));

			// 过滤掉已经存在的数据
			// let newData = res.data.data.filter(item => !existingData.has(item.id));

			// 将新数据添加到项目列表中
			// this.projectList = this.projectList.concat(newData);


		},
		loadNext() {
			if (this.projectList.length >= this.projectListTotal) {
				this.hasMore = false
			}
			if (this.hasMore) {
				this.limit += 10;
				this.getProjectList();
			}
		},
		tabSelect(e) {
			this.TabCur = e.currentTarget.dataset.id;
			this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60

			if (e.currentTarget.dataset.id == 1) {
				this.getProjectList(false)
			}
			if (e.currentTarget.dataset.id == 2) {
				this.getProjectList(true)
			}

		},
		// 当搜索框内为空时重新请求列表
		async InputBlur(e) {
			console.log('e: ', e)
			this.InputBottom = 0
			if (e.detail.value == '') {
				let res = await this.$myHttp.post({
					url: this.$myHttp.urlMap.getProjectList,
					data: {
						name: this.searchName,
						isHis: false,
						page: 1,
						limit: this.limit,
					}
				})

				this.projectList = res.data.data

			}

		},
		gotoDetail(id) {

			uni.navigateTo({
				url: `/subpackage/projectDetail/projectDetail?param1=${id}`
			})
		},
		publishProject() {
			uni.redirectTo({
				url: '/subpackage/publishProject/publishProject'
			})
		}
	}
}
</script>

<style lang="scss">
.no-more {
	color: #999;
}

.cutpage-container {
	padding: 10px;
	color: #026aef;
	display: flex;
	align-items: center;
	justify-content: center;
}

.sendBtn-contaienr {
	// 居中
	position: fixed;
	bottom: 15px;
	right: 130px;
	margin-right: 20rpx;
	margin-bottom: 20rpx;

	image {
		width: 200rpx;
		height: 200rpx;
	}
}

.taskList {
	background-color: #F8F8F8;
	padding: 9px;

	.task-item {
		background-color: #fff;
		margin-bottom: 18rpx;
		padding: 10px;
		border-radius: 10rpx;

		.publisher-container {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;

			.publisher-avator {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				span {
					margin-left: 10rpx;
				}
			}

			.publisher-location {
				display: flex;
				justify-content: flex-end;
				align-items: center;

				text {
					font-size: 12px;
					color: #A8A8A8;
				}
			}
		}

		.item-top {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.item-title {
				font-size: 16px;
				font-weight: bold;
			}

			.item-money {
				font-size: 16px;
				color: #0074D6;
				font-weight: bold;
				background-color: #fff;

				span {
					font-size: 12px;
					margin-left: 5rpx;
					font-weight: normal;
				}
			}
		}

		.item-content {
			margin-top: 20rpx;
			margin-bottom: 20rpx;
		}

		.tagsList {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			font-size: 12px;

			.tags {
				color: #A8A8A8;
				background-color: #EDEDED;
				padding: 2px 5px;
				border-radius: 10rpx;
				margin-right: 10px;
			}
		}
	}
}

.tabSwitch {
	width: 100%;
	background-color: #fff;
	border-radius: 20rpx 20rpx 0 0;
	padding-top: 10rpx;
}

page {
	background-image: linear-gradient(#026aef 0, #fff 180px, #fff 200px, #f9f9f9 230px, #f9f9f9 100%);
	background-repeat: no-repeat;
	background-color: #f9f9f9;
}
</style>